package com.lemon.cloud.datascope;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.lemon.cloud.datascope.handler.DataScopeHandler;
import com.lemon.cloud.datascope.service.DataScopeService;
import com.lemon.cloud.datascope.sqlFilter.OrgScopeSqlFilter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HuangDS
 */
@Component
@Configuration
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
@Import(value = {DataScopeService.class, OrgScopeSqlFilter.class})
public class MybatisDataScopeConfiguration {

    @Bean
    public DataScopeHandler dataScopeHandler(MybatisPlusInterceptor interceptor) {
        DataScopeHandler dataScopeHandler = new DataScopeHandler();
        DataPermissionInterceptor dataPermissionInterceptor = new DataPermissionInterceptor();
        dataPermissionInterceptor.setDataPermissionHandler(dataScopeHandler);
        List<InnerInterceptor> list = new ArrayList<>();
        list.add(dataPermissionInterceptor);
        list.add(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.setInterceptors(list);
        return dataScopeHandler;
    }


}
